import os
from dashscope import MultiModalConversation

# 将xxxx/test.mp4替换为你本地视频的绝对路径
local_path = "E:/pythonproject/smart_insert_video/dtzl.mp4"
video_path = f"file://{local_path}"
print(f"[开始] 读取视频: {video_path}")
messages = [{'role':'user',
             'content': [{'video': video_path},
                         {'text': '请用一句话概述视频内容,要求不得多余50个汉字'}]}]


response = MultiModalConversation.call(
    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
    api_key="sk-4279edafb3314006bf64d4bb94be2e4e",
    model="qvq-max",  # 此处以qvq-max为例，可按需更换模型名称。
    messages=messages,
    stream=True,
)

# 定义完整思考过程
reasoning_content = ""
# 定义完整回复
answer_content = ""
# 判断是否结束思考过程并开始回复
is_answering = False

print("=" * 20 + "思考过程" + "=" * 20)

for chunk in response:
    # 如果思考过程与回复皆为空，则忽略
    print(chunk)
    message = chunk.output.choices[0].message
    reasoning_content_chunk = message.get("reasoning_content", None)

    if (chunk.output.choices[0].message.content == [] and
            reasoning_content_chunk == ""):
        pass
    else:
        # 如果当前为思考过程
        if reasoning_content_chunk != None and chunk.output.choices[0].message.content == []:
            print(chunk.output.choices[0].message.reasoning_content, end="")
            reasoning_content += chunk.output.choices[0].message.reasoning_content
        # 如果当前为回复
        elif chunk.output.choices[0].message.content != []:
            if not is_answering:
                print("\n" + "=" * 20 + "完整回复" + "=" * 20)
                is_answering = True
            print(chunk.output.choices[0].message.content[0]["text"], end="")
            answer_content += chunk.output.choices[0].message.content[0]["text"]

# 如果您需要打印完整思考过程与完整回复，请将以下代码解除注释后运行
print("=" * 20 + "完整思考过程" + "=" * 20 + "\n")
print(f"{reasoning_content}")
print("=" * 20 + "完整回复" + "=" * 20 + "\n")
print(f"{answer_content}")